//
// $Id: X11WindowHandler.m 6 2010-08-05 19:43:27Z principiadriver $
//
// Written by
//  Michael Klein <michael.klein@puffin.lb.shuttle.de>
//
// This file is part of Alternate Moves.
//
// This  is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#import "../Helper/WindowHandler.h"

#define Cursor X11_Cursor
# include <X11/Xlib.h>
#undef Cursor


// static, because XOpenDisplay/XCloseDisplay leaks 128 bytes :-(
static Display *display;


@interface X11WindowHandler : NSObject<WindowHandler>
{
    unsigned int window;
}

@end


@implementation X11WindowHandler

- (id)initWithX11Window:(Window)x11window
{
    if ((self = [super init]) != nil) {
        //NSLog(@"init %@ (Window=%08x)", self, x11window);
        window  = x11window;
    }

    return self;
}


- (void)dealloc
{
    //NSLog(@"dealloc %@ (Window=%08x)", self, window);
    [super dealloc];
}


+ (id)newWindowHandlerForWindowAtPoint:(CGPoint)loc
{
    if (!display)
        display = XOpenDisplay(NULL);

    if (display) {
        //NSLog(@"opened display %p", display);
        Window root, window;
        int root_x, root_y, win_x, win_y;
        unsigned int mask;
        if (XQueryPointer(display, DefaultRootWindow(display), &root,
                          &window, &root_x, &root_y, &win_x, &win_y, &mask) &&
            window != None) {
            return [[X11WindowHandler alloc] initWithX11Window:window];
        }
    }
    return nil;
}

- (CGRect)setWindowRect:(CGRect)newRect
{
    XResizeWindow(display, window, newRect.size.width, newRect.size.height);
    XMoveWindow(display, window, newRect.origin.x, newRect.origin.y);

    return [self windowRect];
}

- (CGRect)windowRect
{
    Window root;
    int x, y;
    unsigned int w, h, bw, d;
    if (XGetGeometry(display, window, &root, &x, &y, &w, &h, &bw, &d))
        return CGRectMake(x, y, w + bw, h + bw);
    else
        return CGRectZero;
}


- (NSString*)windowTitle
{
    char *name = NULL;
    if (!XFetchName(display, window, &name)) {
        Window root, parent, *children;
        unsigned int nchildren;
        if (XQueryTree(display, window, &root, &parent, &children, &nchildren)) {
            int i;
            for (i = 0; i < nchildren && !name; i++) {
                XFetchName(display, children[i], &name);
            }
            XFree(children);
        }
    }

    if (name) {
        NSString *ret = [NSString stringWithUTF8String:name];
        XFree(name);
        return ret;
    }

    return @"";
}


@end
